package com.kkm.filter;

import com.kkm.pojo.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 过滤器 过滤了登录后的用户的信息管理
 */

@WebFilter("/index.jsp")
public class filterDemo implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        System.out.println("doFilter.....在过滤器中打印的");
        HttpServletRequest httpRequest= (HttpServletRequest) request;
        HttpSession session=httpRequest.getSession();
        Admin admin= (Admin) session.getAttribute("Admin");
        if(admin!=null){
            chain.doFilter(request,response);
            System.out.println("有登录记录，在过滤器中打印的");
            return;
        }else {
            request.getRequestDispatcher("adminLogin.jsp").forward(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}
